/*
 * Copyright (C) 2015 备胎金服
 * 未经授权允许不得进行拷贝和修改
 *   http://www.btjf.com/
 */
package com.zhangman.androidgame;

import com.zhangman.framework.Image;
import java.util.ArrayList;

/**
 * Created by zhangman on 2016/11/1 16:07.
 * Email: zhangman523@126.com
 */
public class Animation {
  private ArrayList frames;
  private int currentFrame;
  private long animTime;
  private long totalDuration;

  public Animation() {
    frames = new ArrayList();
    totalDuration = 0;
    synchronized (this) {
      animTime = 0;
      currentFrame = 0;
    }
  }

  public synchronized void addFrame(Image image, long duration) {
    totalDuration += duration;
    frames.add(new AnimFrame(image, totalDuration));
  }

  public synchronized void update(long elapsedTime) {
    if (frames.size() > 1) {
      animTime += elapsedTime;
      if (animTime >= totalDuration) {
        animTime = animTime % totalDuration;
        currentFrame = 0;
      }
      while (animTime > getFrame(currentFrame).endTime) {
        currentFrame++;
      }
    }
  }

  public synchronized Image getImage() {
    if (frames.size() == 0) {
      return null;
    } else {
      return getFrame(currentFrame).image;
    }
  }

  private AnimFrame getFrame(int i) {
    return (AnimFrame) frames.get(i);
  }

  private class AnimFrame {
    Image image;
    long endTime;

    public AnimFrame(Image image, long endTime) {
      this.endTime = endTime;
      this.image = image;
    }
  }
}
